package nit.wave.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
import nit.wave.dao.DataBaseDAO;
import nit.wave.model.Student;
import nit.wave.utils.HttpRequest;
import nit.wave.utils.SignUtil;
import nit.wave.utils.WeixinUtil;

public class LibraryService {

	public String binding(String openId, String userId, String password) {
		try {
			int count = DataBaseDAO.findUserByOpenId("lib_user", openId);
			if (count == 0) {
				int countUser = DataBaseDAO.findUserByuSserId("lib_user", userId);
				if (countUser == 0) {
					String result  = this.login(userId, password);
					JSONObject node = JSONObject.fromObject(result);
					int code = node.getInt("code");
					if (code == 200) {
						try {
							if (DataBaseDAO.intsertUser("lib_user", new Student(openId, userId, password)))
								return "绑定成功";
						} catch (Exception e) {
							e.printStackTrace();
						}
					}else if(code == 202){
						return "帐号或密码错误";
					}else{
						return "服务器出错啦！请联系管理员";
					}
				} else {
					return "您的帐号已在其他设备绑定";
				}
			} else {
				return "您的帐号已绑定";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "";
	}

	private String login(String userId, String password) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("secret", WeixinUtil.LIB_SECRET);
		param.put("userID", userId);
		param.put("userPwd", password);
		return HttpRequest.sendPost("http://nitlib.hiunique.com/LoginController/login", param);
	}
	
	public String getBook(String openId) {
		String result = null;
		try {
			Student student = DataBaseDAO.getStudent("lib_user", openId);
			System.out.println(student);
			if (student != null) {
				System.out.println(student.getOpenId());
				Map<String, String> param = new HashMap<String, String>();
				param.put("userID", student.getUserId());
				param.put("userPwd", student.getPassword());

				param.put("secret", SignUtil.toSHA(WeixinUtil.LIB_SECRET, student.getUserId()));
				result = HttpRequest.sendPost("http://nitlib.hiunique.com/BookController/getBook", param);
			} else {
				return "你还没有绑定过图书馆";
			}
		} catch (Exception e) {
			e.getStackTrace();
		}
		return result;
	}

	public String changePassword(String openId, String password) {
		String result = null;
		try {
			Student student = DataBaseDAO.getStudent("lib_user", openId);
			Map<String, String> param = new HashMap<String, String>();
			param.put("userID", student.getUserId());
			param.put("userPwd", student.getPassword());
			param.put("oldPwd", student.getPassword());
			param.put("newPwd", password);
			param.put("secret", SignUtil.toSHA(WeixinUtil.LIB_SECRET, student.getUserId()));
			result = HttpRequest.sendPost("http://nitlib.hiunique.com/LoginController/topwdmodify", param);
			JSONObject node = JSONObject.fromObject(result);
			int code = node.getInt("code");
			if (code == 200) {
				DataBaseDAO.updatePassword("lib_user", openId, password);
				return "修改成功";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public String getPassword(String openId) {
		String result = null;
		try {
			Student student = DataBaseDAO.getStudent("lib_user", openId);
			Map<String, String> param = new HashMap<String, String>();
			param.put("userID", student.getUserId());
			param.put("secret", SignUtil.toSHA(WeixinUtil.LIB_SECRET, student.getUserId()));
			result = HttpRequest.sendPost("http://nitlib.hiunique.com/LoginController/getpwd", param);
			JSONObject node = JSONObject.fromObject(result);
			int code = node.getInt("code");
			if (code == 200) {
				return node.getString("message");
			} else {
				return "密钥错误";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public String renewBook(String openId, String bookId, String check) {
		String result = null;
		try {
			Student student = DataBaseDAO.getStudent("lib_user", openId);
			Map<String, String> param = new HashMap<String, String>();
			param.put("userID", student.getUserId());
			param.put("userPwd", student.getPassword());
			param.put("bookID", bookId);
			param.put("check", check);
			param.put("secret", SignUtil.toSHA(WeixinUtil.LIB_SECRET, student.getUserId()));
			result = HttpRequest.sendPost("http://nitlib.hiunique.com/BookController/renewBook", param);
			JSONObject node = JSONObject.fromObject(result);
			int code = node.getInt("code");
			if (code == 200) {
				return node.getString("message");
			} else if(code == 202) {
				return "条码号或校验码错误";
			}else if(code == 204){
				return "缺少参数";
			}else {
				return "密钥错误";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
